package com.xunshangwang.pinhaohuo.http;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.SyncHttpClient;
import com.xunshangwang.pinhaohuo.app.MainApplication;
import com.xunshangwang.pinhaohuo.tools.DebugHelper;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.security.Policy;

import cz.msebera.android.httpclient.entity.StringEntity;

/**
 * Created by xzp on 15/9/30.
 */
public class HttpTool {
    private static HttpTool ourInstance = new HttpTool();
    //private static final String TEST_URL="http://app.xun333.com/";
    //private Context mContext;
    private static final String IMAGE_URL="http://app.xun333.com";
    private static final String BASE_URL="http://pinduoduo.xunshangwang.cn/index.php";
    public static HttpTool getInstance() {
        return ourInstance;
    }
    private AsyncHttpClient mAsyncClient;
    private SyncHttpClient mSyncClient;
    private HttpTool() {
        mAsyncClient=new AsyncHttpClient();
        mAsyncClient.setTimeout(10000);
        mAsyncClient.setMaxRetriesAndTimeout(1,10000);
        mSyncClient=new SyncHttpClient();
        mSyncClient.setTimeout(10000);
    }

//    public static final void init(Context context){
//        ourInstance.mContext=context;
//    }

    public static String getAbsoluteUrl(String relativeUrl)
    {
        if(relativeUrl==null)
            return null;
        return BASE_URL+relativeUrl;
    }
    public static String getImageUrl(String relativeUrl)
    {
        return IMAGE_URL+relativeUrl;
    }
    public static void get(Context context,String url,ResponseHandlerInterface handler)
    {
        DebugHelper.i("REQUEST_URL",BASE_URL+url);
        ourInstance.mAsyncClient.get(context, BASE_URL + url, handler);
    }
    public static void post(Context context,String url,RequestParams params,ResponseHandlerInterface handler)
    {
        DebugHelper.i("REQUEST_URL",BASE_URL+url);
        DebugHelper.i("REQUEST_PARAMS",params.toString());
        ourInstance.mAsyncClient.post(context, BASE_URL + url, params, handler);

    }
    public static final void postJson(Context context,String url,JSONObject json,ResponseHandlerInterface handler){
        DebugHelper.i("REQUEST_URL",BASE_URL+url);
        DebugHelper.i("REQUEST_JSON",json.toString());
        StringEntity entity=new StringEntity(json.toString(),"UTF-8");
        ourInstance.mAsyncClient.post(context,BASE_URL+url,entity,"application/json",handler);
    }
    public static final void putJson(Context context,String url,JSONObject json,ResponseHandlerInterface handler){
        DebugHelper.i("REQUEST_URL",BASE_URL+url);
        DebugHelper.i("REQUEST_JSON",json.toString());
        StringEntity entity=new StringEntity(json.toString(),"UTF-8");
        ourInstance.mAsyncClient.put(context, BASE_URL + url, entity, "application/json", handler);
    }
    public static final void delete(Context context,String url,ResponseHandlerInterface handler){
        DebugHelper.i("REQUEST_URL",BASE_URL+url);
        ourInstance.mAsyncClient.delete(context,BASE_URL+url,handler);
    }
    public static void cancelRequests(Context context)
    {
        ourInstance.mAsyncClient.cancelRequests(context, true);
    }
    public static void cancelRequests(Object tag)
    {
        ourInstance.mAsyncClient.cancelRequestsByTAG(tag,true);
    }
    public SyncHttpClient getSyncClient()
    {
        return mSyncClient;
    }
}
